﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BTKERP.IBLL.Inventory;
using BTKERP.BLL.Inventory;
using BTKERP.Model;
using BTKERP.MVCWeb.AppCode;

namespace BTKERP.MVCWeb.Controllers
{
    public class UnitController : RestrictController
    {
        //
        // GET: /Unit/
        public ActionResult Index()
        {
            List<Unit> units = InventoryInfoMgt.GetUnits();

            ViewData["unittype"] = GetUnitTypeList();
            return View(units);
        }

        //
        // GET: /Unit/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Unit/Create

        public ActionResult Create()
        {
            Unit unit = new Unit();
            ViewData["UnitTypeList"] = GetUnitTypeSelectList();
            return View(unit);
        } 

        //
        // POST: /Unit/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Unit unit = new Unit();
                UpdateModel(unit);
                InventoryInfoMgt.AddUnit(unit.Name, unit.Description, unit.UnitTypeId, SessionEntity.Staff.UserName);
                DataRefHelper.UpdateUnits();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Unit/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            ViewData["UnitTypeList"] = GetUnitTypeSelectList();
            Unit unit = InventoryInfoMgt.GetUnitById(id);
            return View(unit);
        }

        //
        // POST: /Unit/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Unit unit = new Unit();
                UpdateModel(unit);
                InventoryInfoMgt.UpdateUnit(unit.Id, unit.Name, unit.Description, unit.UnitTypeId, SessionEntity.Staff.UserName);
                DataRefHelper.UpdateUnits();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Unit/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Unit/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
